package group32.client;
import java.util.ArrayList;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CaptionPanel;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class Circle extends Composite {
	int userid=-1;
	private CircleServiceAsync pService = GWT
	.create(CircleService.class);
	public Circle(final int userID, final String username){
		final VerticalPanel finalpanel = new VerticalPanel();
		finalpanel.setSpacing(10);
		initWidget(finalpanel);
		    final Label lbl2 = new Label("Circles");
		    final Button btn3=new Button("Add a circle");
		    final DialogBox dialogBox = new DialogBox();
	  		final HTML serverResponseLabel = new HTML();
	  		final Button closeButton = new Button("Close");
	  		final Anchor homepage = new Anchor();
	  		homepage.setText("Home");
	  		pService.getRelation(userID,new AsyncCallback<ArrayList<String>>(){ 

	  			@Override
	  			public void onFailure(Throwable caught) {
	  				dialogBox.setText("Remote Procedure Call - Failure");
	  				serverResponseLabel.addStyleName("serverResponseLabelError");
	  				serverResponseLabel.setHTML("SERVER_ERROR");
	  				dialogBox.show();
	  				dialogBox.center();
	  				closeButton.setFocus(true);
	  			}

	  			@Override
	  			public void onSuccess(ArrayList<String> result) {
	  				// Add the relational schema to the output
	  				 final VerticalPanel Panel= new VerticalPanel();
	  				Panel.setSpacing(10);
	  				final VerticalPanel HPanel= new VerticalPanel();
	  				HPanel.setSpacing(40);
	  				final VerticalPanel H2Panel= new VerticalPanel();
	  				H2Panel.setSpacing(10);
	  				final HorizontalPanel FPanel= new HorizontalPanel();
	  				FPanel.setSpacing(10);
	  				 //final VerticalPanel V1Panel= new VerticalPanel();
	  				//lbl.setText(result);
	  			   Panel.add(homepage);
	  			   Panel.add(lbl2);
	  			   Panel.add(btn3);
	  				homepage.addClickHandler(new ClickHandler() {
	  			        public void onClick(ClickEvent event) {
	  			            FlowControl.go(new HomePage(userID, username));             
	  			        }           
	  			    });
	  			 
	  				for(int i=0;i<result.size();i++)
		  				{
	  					String cname =result.get(i);
	  					final Label lname = new Label(cname);
		  				//Panel.add(new HTML (result.get(i)));
		  				final Anchor anchor = new Anchor("View Friends");
		  		        final Button btn2=new Button("Delete");
		  				HPanel.add(lname);
		  				H2Panel.add(anchor);
		  				H2Panel.add(btn2);
		  			    FPanel.add(HPanel);
		  			    FPanel.add(H2Panel);
		  				final String cname1=result.get(i);
		  				 btn2.addClickHandler(new ClickHandler() { 
		  				@Override
	  					public void onClick(ClickEvent event) {
		  					boolean status=false;
		  					if (!status)
		  					{
	  						//Window.confirm("Do you want to delete all the friends in the circles!!!");
	  						if (Window.confirm("Do you want to delete all the friends in the circles!!!"))
	  							{
			  		  	       deleteCircle(cname1,userID);
			  		  	   lname.setVisible(false);
		  		  	       anchor.setVisible(false);
		  		  	       btn2.setVisible(false);
	  							}
			  		  	       else
			  		  	       {
			  		  	    	   Window.alert("deletion canceled");
			  		  	       }
			  		  	    	   }
		  					}});
		  				 
		  				
		  				
		  				
	  				anchor.addClickHandler(new ClickHandler() { public void onClick(ClickEvent event) {         
	  		  	       loadlinkProfile(cname1);}

	  				private void loadlinkProfile(String name) {
	  					
	  					finalpanel.clear();
	  			 		  	  // add a new app name
	  					finalpanel.add(new Label("Friends")); 
	  					finalpanel.add(new Label()); 
	  			 		  	
	  			 		 // 	 RootPanel.get("gwt-anchor").clear();
	  			 		  
	  			 		  	VerticalPanel NewVPanel = new VerticalPanel();
	  			 		 pService.insertFriends(userID,name,new AsyncCallback<ArrayList<String>>(){ 

	  			 	  			
	  			 	  			public void onFailure(Throwable caught) {
	  			 	  				// Show the RPC error message to the user
	  			 	  				
	  			 					dialogBox
	  			 	  						.setText("Remote Procedure Call - Failure");
	  			 	  				serverResponseLabel
	  			 	  						.addStyleName("serverResponseLabelError");
	  			 	  				serverResponseLabel.setHTML("SERVER_ERROR");
	  			 	  				dialogBox.show();
	  			 	  				dialogBox.center();
	  			 	  				
	  			 	  				closeButton.setFocus(true);
	  			 	  			}
	  			 	  			
	  			 	  			public void onSuccess(ArrayList<String> result) {
	  			 	  				// Add the relational schema to the output
	  			 	  				 final VerticalPanel VPanel = new VerticalPanel();
	  			 	  				 VPanel.setSpacing(10);
	  			 	  				for(int i=0;i<result.size();i++)
	  			 	  				{
	  			 	  				VPanel.add(new Anchor (result.get(i)));
	  			 	  				
	  			 	  				}

	  			 	  				finalpanel.add(VPanel);
	} 
	  			  	   });  	
	  			 		  	
	  					
	  				} 
	  				});
	  		  		}

	  				btn3.addClickHandler(new ClickHandler() { public void onClick(ClickEvent event) {         
	  					createcirclePopup();
	  				}
	   		  	   }); 

	  				finalpanel.add(Panel);
	  				finalpanel.add(FPanel);
	  				
		}
		  
	  			
	  			public void createcirclePopup() {
	  				// Create the popup dialog box
	  				final DialogBox dialogBox = new DialogBox();
	  				dialogBox.setText("Add New Circle");
	  				dialogBox.setAnimationEnabled(true);
	  				final Button closeButton = new Button("Close");
	  				// We can set the id of a widget by accessing its Element
	  				closeButton.getElement().setId("closeButton");
//	  				final HTML schemaLabel = new HTML();
//	  				final HTML serverResponseLabel = new HTML();
	  				VerticalPanel dialogVPanel = new VerticalPanel();
	  				dialogVPanel.addStyleName("dialogVPanel");
	  				dialogVPanel.add(new HTML("<br><b>Circle name:</b>"));
	  				//dialogVPanel.add(schemaLabel);
	  				final TextBox circlename= new TextBox();
	  				//dialogVPanel.add(new HTML("<br><b>Returned data:</b>"));
	  				dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT);
	  				dialogBox.setWidget(dialogVPanel);
	  				final Button addcir = new Button("Add Circle");
	  				dialogVPanel.add(circlename);
	  				dialogVPanel.add(addcir);
	  				dialogVPanel.add(closeButton);
	  				dialogBox.show();
					dialogBox.center();
					final String textval=circlename.getText();
					
					closeButton.setFocus(true);
					closeButton.addClickHandler(new ClickHandler() {

						@Override
						public void onClick(ClickEvent event) {
							dialogBox.hide();
						}
						
					});
	  				  
	  				addcir.addClickHandler(new ClickHandler() {
	  					
	  					@Override
	  					public void onClick(ClickEvent event) {
	  						if (circlename.getText().isEmpty()) {
	  							Window.alert("Sorry, you must enter a circle to add");
	  						} else
	  							callCircles(textval); 
	  					}

						public void callCircles(String textval) {
							String cirname=textval;
							final HTML serverResponseLabel = new HTML();
							pService.insertCircles(cirname,userID ,new AsyncCallback<Void>(){
				                   
								public void onFailure(Throwable caught) {
										// Show the RPC error message to the user
										dialogBox
												.setText("Remote Procedure Call - Failure");
										serverResponseLabel
												.addStyleName("serverResponseLabelError");
										serverResponseLabel.setHTML("SERVER_ERROR");
										dialogBox.show();
										dialogBox.center();
										
										closeButton.setFocus(true);
									}

								public void onSuccess(Void result) {
										// Add the relational schema to the output
				                	   Window.alert("Insertion completed successfully");
										dialogBox.show();
										dialogBox.center();
										dialogBox.hide();
								}

							
							});
							
						
							
						}

						 
	  				});
	  				
	  			}
	  	//new function start	
	  			public void deleteCircle(String cname1, int id) {
	  				// Create the popup dialog box
	  				final DialogBox dialogBox = new DialogBox();
	  				final HTML serverResponseLabel = new HTML();
	  				final Button closeButton = new Button("Close");
	  				pService.delCir(cname1,id ,new AsyncCallback<Void>(){
		                   
						public void onFailure(Throwable caught) {
								// Show the RPC error message to the user
								dialogBox
										.setText("Remote Procedure Call - Failure");
								serverResponseLabel
										.addStyleName("serverResponseLabelError");
								serverResponseLabel.setHTML("SERVER_ERROR");
								dialogBox.show();
								dialogBox.center();
								
								closeButton.setFocus(true);
							}

						public void onSuccess(Void result) {
								// Add the relational schema to the output
		                	   Window.alert("deletion completed successfully");
		                	   
								dialogBox.show();
								dialogBox.center();	
						}

					
					});
	  			}
// new function end
		});     //getRelation closing 
		    	    
	}
	}

